package com.sun.chart;

import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import com.sun.base.UserActivity;
import com.tools.HttpInterface;
import com.tools.SysInterfacePhone;

public class ShowChartImageActivity extends UserActivity
{

	private String address;
//	private ScrollView scrollView;
	private ImageView imageView;
	private ProgressBar progressBar;
	private RelativeLayout contentLayout;

	@Override
	public void init()
	{
		Intent intent = getIntent();
		address = intent.getStringExtra("address");
		super.setTitle(intent.getStringExtra("name"));
		contentLayout = new RelativeLayout(this);
		SysInterfacePhone.invoker().setViewBackground(this, titleLayout, "titlebackground.png");
		SysInterfacePhone.invoker().setViewBackground(this, contentLayout, "back_ground.png");
		mainLayout.addView(contentLayout, new RelativeLayout.LayoutParams(-1, -1));
//		scrollView = new ScrollView(this);
		imageView = new ImageView(this);
//		scrollView.addView(imageView,new ViewGroup.LayoutParams(-1,-1));
		progressBar = new ProgressBar(this);
		RelativeLayout.LayoutParams scrollParams = new RelativeLayout.LayoutParams(-1, -1);
		scrollParams.setMargins(12, 12, 12, 12);
		contentLayout.addView(imageView, scrollParams);
		RelativeLayout.LayoutParams progressBarParams = new RelativeLayout.LayoutParams(-2, -2);
		progressBarParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		contentLayout.addView(progressBar, progressBarParams);
		new Thread()
		{

			public void run()
			{
				HttpInterface interface1 = new HttpInterface();
				try
				{
					final HttpEntity entity = interface1.httpConnection(20, address, HttpInterface.HTTP_GET_METHOD, null, null, 0);
					handler.post(new Runnable()
					{

						@Override
						public void run()
						{
							if (entity == null)
							{
								Builder dialog = new AlertDialog.Builder(ShowChartImageActivity.this);
								dialog.setMessage("获取走势图失败");
								dialog.create().show();
								return;
							}
							InputStream is = null;
							try
							{
								is = entity.getContent();
								imageView.setImageBitmap(BitmapFactory.decodeStream(is));
								progressBar.setVisibility(View.GONE);
							}
							catch (Exception e)
							{
								e.printStackTrace();
							}
							finally
							{
								try
								{
									is.close();
									entity.consumeContent();
								}
								catch (Exception e2)
								{
									e2.printStackTrace();
								}
							}
						}
					});
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}.start();
	}
}
